<?php
require_once ('../../inc/functions.php');

class ProductCategory
{
	static function GetAll()
	{
		$conn = GetConnection();	
		return $conn->query('SELECT * FROM 2012Grad_ProductCategory');
		//$row = $response->fetch_assoc();
		//print_r($row);
	}
	
	static function Get($id)
	{
		$conn = GetConnection();	
		$results = $conn->query("SELECT * FROM 2012Grad_ProductCategory WHERE id=$id");
		$row = $results->fetch_assoc();
		$conn->close();
		return $row;
	}
	
	static function Insert($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
        $sql =  "INSERT INTO 2012Grad_ProductCategory "
        		. "(created_at, updated_at, Name) "
                . "VALUES (Now(), Now(), '$row2[Name]') ";
        //echo $sql;
        $conn->query($sql);
        $error = $conn->error;
        $conn->close();
                
        return $error != '' ? array('Server Error' => $error) : true;           
	}
	
	static function Update($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
        $sql =  "UPDATE 2012Grad_ProductCategory "
                . "Set created_at='$row2[created_at]',updated_at=Now(),Name='$row2[Name]' "
                . "WHERE id=$row2[id] ";
        //echo $sql;
        $conn->query($sql);
        $error = $conn->error;
        $conn->close();
                
        return $error != '' ? array('Server Error' => $error) : true;           
	}
	
	static function Delete($row)
	{
		$conn = GetConnection();
		$sql = "DELETE FROM 2012Grad_ProductCategory "
				. "WHERE id=$row[id] ";
		$conn->query($sql);
		$error = $conn->error;
		
		return $error != '' ? array('Server Error' => $error) : true;
	}
	
	static function Validate($row)
	{
		$results = array();
		if(empty($row['Name'])) $results['Name'] = 'Name is required';
		
		return count($results) > 0 ? $results : true;
	}
	
}